package fr.enseirb.mycub.utils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Vector;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.location.Location;
import android.net.Uri;

import fr.enseirb.mycub.activity.AbstractPreferencesActivity;
import fr.enseirb.mycub.db.BikeDBConstants;
import fr.enseirb.mycub.db.DBAdapter;
import fr.enseirb.mycub.service.LocationService;
import fr.enseirb.mycub.structures.Station;

public class Utils {

	public static final String FAVORITE_WHERE_CLAUSE = BikeDBConstants.KEY_FAVORITE + " = 1";

	static public void sortStationsByDistance(
			List<? extends Station> list) {
		Collections.sort(list, new Comparator<Station>() {
			@Override
			public int compare(Station s1, Station s2) {
				if (s1.getDistance() < s2.getDistance()) {
					return -1;
				} else if (s1.getDistance() > s2.getDistance()) {
					return 1;
				} else {
					return 0;
				}
			}
		});
	}

	static public String whereClauseFromFilter(SharedPreferences preferences) {
		Vector<String> selection = new Vector<String>();
		if (preferences.getBoolean(AbstractPreferencesActivity.FAVORITE_FILTER, false))
			selection.add("(" + BikeDBConstants.KEY_FAVORITE + " = 1 )");
		if (preferences.getBoolean(AbstractPreferencesActivity.BIKES_FILTER, false))
			selection.add("(" + BikeDBConstants.KEY_BIKES + " != 0 )");
		if (preferences.getBoolean(AbstractPreferencesActivity.SLOTS_FILTER, false))
			selection.add("(" + BikeDBConstants.KEY_SLOTS + " != 0 )");
		int size = selection.size();
		if (size == 0)
			return null;
		String where = selection.firstElement();
		for (int i = 1; i < selection.size(); i++) {
			where += " AND " + selection.elementAt(i);
		}
		return where;
	}

	static public String formatDistance(int distance) {
		int km = distance / 1000;
		int m = distance % 1000;
		return km == 0 ? m + "m" : km + "," + m + "km ";
	}

	static public boolean isIntentAvailable(Intent intent, Context context) {
		final PackageManager packageManager = context.getPackageManager();
		List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
				PackageManager.MATCH_DEFAULT_ONLY);
		return list.size() > 0;
	}

	public static int computeDistance(int latitude, int longitude, Location from) {
		if (from == null) {
			return LocationService.DISTANCE_UNAVAILABLE;
		}
		Location l = new Location("");
		l.setLatitude((double) latitude * 1E-6);
		l.setLongitude((double) longitude * 1E-6);
		return (int) from.distanceTo(l);
	}

	public static Intent getNavigationIntent(int latitude, int longitude) {
		return new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q="
				+ latitude * 1E-6 + "," + longitude * 1E-6 + "&mode=w"));
	}

	public static Intent getMapsIntent(int latitude, int longitude, String label) {
		return new Intent(Intent.ACTION_VIEW, Uri.parse("geo:0,0?q=" + latitude
				* 1E-6 + "," + longitude * 1E-6 + " (" + label + ")"));
	}

	public static String StringTime(long lastUpdate, Activity activity){
		long delai = System.currentTimeMillis() - lastUpdate;
		String format = String.format("%%0%dd", 2);
		delai = delai / 1000;
		if(delai<1 || delai/3600 > 72) return "";
		String seconds = String.format(format, delai % 60);
		String minutes = String.format(format, (delai % 3600) / 60);
		String hours = String.format(format, delai / 3600);
		String time = " (";
		//time += "il y a ";
		//time += activity.getString(R.string.since);
		if(delai / 3600 > 0) time += hours+ " h";
		else if(((delai % 3600) / 60)> 0) time += minutes+" min";
		else if(delai % 60> 0) time += seconds+" s";
		time +=")";
		return time;
	}

	public static int[] convertStringArrayListtoIntArray(List<String> items) throws Exception {
		if (items != null) {
			int intarray[] = new int[items.size()];
			for (int i = 0; i < items.size(); i++) {
				intarray[i] = Integer.parseInt(items.get(i));
			}
			return intarray;
		}
		return null;
	}
}
